package com.shangxian.giveithome.utils;

import com.shangxian.giveithome.code.CommonCode;
import com.shangxian.giveithome.code.UserCode;
import com.shangxian.giveithome.entity.Mechanism;
import com.shangxian.giveithome.entity.UserData;
import com.shangxian.giveithome.entity.UserDetails;
import com.shangxian.giveithome.exception.ExceptionUtils;
import com.shangxian.giveithome.mapper.NewReportMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisUtils redisUtils;

    @Resource
    private NewReportMapper newReportMapper;

    /**
     * 获取用户信息
     *
     * @param request
     * @return
     */
    public UserDetails getHeader(HttpServletRequest request) {
        String header = request.getHeader("Set-Cookie");
        if (StringUtils.isBlank(header)) {
//            ExceptionUtils.error(CommonCode.AUTH_NOT_SUCCESS);
            return null;
        }
        if (redisTemplate.hasKey(header)) {
            UserDetails userDetails = (UserDetails) redisTemplate.opsForValue().get(header);
            return userDetails;
        }
        ExceptionUtils.error(CommonCode.AUTH_NOT_SUCCESS);
        return null;
    }

    public void upUserDetails(HttpServletRequest request, UserDetails userDetails) {
        String header = request.getHeader("Set-Cookie");
        if (StringUtils.isBlank(header)) {
            ExceptionUtils.error(CommonCode.AUTH_NOT_SUCCESS);
        }
        if (redisTemplate.hasKey(header)) {
            redisTemplate.delete(header);
            redisTemplate.opsForValue().set(header, userDetails, 7L, TimeUnit.DAYS);
            return;
        }
        ExceptionUtils.error(CommonCode.TOKEN_ERROR);
    }

    // 更新redis里用户详情的用户数据
    public void updateUserData(HttpServletRequest request, UserData userData) {
        String header = request.getHeader("Set-Cookie");
        if (StringUtils.isBlank(header)) {
            ExceptionUtils.error(CommonCode.AUTH_NOT_SUCCESS);
        }
        if (redisTemplate.hasKey(header)) {
            UserDetails userDetails = (UserDetails) redisTemplate.opsForValue().get(header);
            userDetails.setUserData(userData);
            redisTemplate.opsForValue().set(header, userDetails, 7L, TimeUnit.DAYS);
            return;
        }
        ExceptionUtils.error(CommonCode.TOKEN_ERROR);
    }

    // 更新redis里用户详情的机构数据
    public void updateMechanism(HttpServletRequest request, Mechanism mechanism) {
        String header = request.getHeader("Set-Cookie");
        if (StringUtils.isBlank(header)) {
            ExceptionUtils.error(CommonCode.AUTH_NOT_SUCCESS);
        }
        if (redisTemplate.hasKey(header)) {
            UserDetails userDetails = (UserDetails) redisTemplate.opsForValue().get(header);
            userDetails.setMechanism(mechanism);
            redisTemplate.opsForValue().set(header, userDetails, 7L, TimeUnit.DAYS);
            return;
        }
        ExceptionUtils.error(CommonCode.TOKEN_ERROR);
    }

    public boolean queryKey(String key) {
        return redisTemplate.hasKey(key);
    }


    public Long getMechanismCheck(HttpServletRequest request) {
        UserDetails header = getHeader(request);
        return getMechanismCheck(header);
    }

    public Long getMechanismCheck(UserDetails userDetails) {
        if (userDetails.getMechanismStatus() && userDetails.getRole() == 3) {
            return userDetails.getMechanism().getId();
        }
        return -1L;
    }

    /**
     * 检查用户登录状态
     *
     * @param request
     * @return
     */
    public void checkStatus(HttpServletRequest request) {
        String header = request.getHeader("Set-Cookie");
        UserDetails userDetails = redisUtils.getHeader(request);
        //检查用户是否为禁用用户
        if (userDetails != null) {
            int count = newReportMapper.IsBlock(Long.parseLong(redisUtils.getHeader(request).getPhone()));
            if (count > 0) {
                ExceptionUtils.error(UserCode.USER_IS_AUDIT);
            }
        }
        if (StringUtils.isBlank(header)) {
            ExceptionUtils.error(CommonCode.AUTH_NOT_SUCCESS);
        }
        if (!redisTemplate.hasKey(header)) {
            ExceptionUtils.error(CommonCode.TOKEN_ERROR);
        }
    }

    public <T> void setKey(String key, T o) {
        redisTemplate.opsForValue().set(key, o);
    }

    public <T> T getRedis(String key) {
        return (T) redisTemplate.opsForValue().get(key);
    }

    // 删除前缀为prefix的key
    public void deleteByPrefix(String prefix) {
        Set<String> keys = redisTemplate.keys(prefix + "*");
        redisTemplate.delete(keys);
    }


}
